package cn.dankal.flyaudio.presenter;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import api.ShopUserServiceFactory;
import cn.dankal.basiclib.pojo.BaseListResponse;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.CommentsEntity;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.flyaudio.contract.CommentsListContract;

/**
 * <pre>
 *     e-mail : junfeng.nie@duntech.com.cn
 *     time   : 21/7/2020
 *     desc   :
 *     version: cn.dankal.flyaudio.presenter
 *     Copyright: Copyright（c）2018
 *     Company:
 * </pre>
 *
 * @author niejunfeng
 */
public class CommentsListPresenter implements CommentsListContract.Presenter {
    private CommentsListContract.View mView;

    @Override
    public void getEvaluate(String uuid, int page) {
        mView.showLoadingDialog();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("productUuid", uuid);
        queryMap.put("page", page);
        queryMap.put("size", "10");
        ShopUserServiceFactory.comments(queryMap)
                .subscribe(new AbstractSubscriber<BaseResponse<BaseListResponse<CommentsEntity>>>(mView) {
                    @Override
                    public void onNext(BaseResponse<BaseListResponse<CommentsEntity>> baseListResponseBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.evaluateSuccess(baseListResponseBaseResponse.getData().getList());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.dismissLoadingDialog();
                    }
                });
    }

    @Override
    public void like(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.likeCommentsProduct(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> objectBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.likeSuccess();
                    }
                });
    }

    @Override
    public void unLike(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.unLikeCommentsProduct(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> objectBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.unLikeSuccess();
                    }
                });
    }

    @Override
    public void attachView(@NonNull CommentsListContract.View view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        this.mView = null;
    }
}
